<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <div>
                    <el-input v-model="query.keywords" placeholder="关键词" class="handle-input mr10"></el-input>
                    <el-select v-if="departs.length > 1" v-model="query.depart" class="m-2 mr10" placeholder="请选择部门"
                        size="large">
                        <el-option key="0" label="全部部门" value="0" />
                        <el-option v-for="depart in departs" :key="depart.depart_id" :label="depart.depart_name"
                            :value="depart.depart_id" />
                    </el-select>
                    <el-button type="primary" class="m-2" size="small" icon="el-icon-search"
                        @click="handleSearch">搜索</el-button>
                </div>
                <div>
                    <!-- <el-button type="primary" size="small" icon="el-icon-plus" @click="handleCreate" class="add-btn">
                        添加病人</el-button> -->
                </div>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header"
                :cell-style="{padding:'3px 0'}">
                <el-table-column prop="alarm_id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="depart_name" label="部门" align="center"></el-table-column>
                <el-table-column label="操作" width="390" align="center">
                    <template #default="scope">
                        <el-button type="primary" size="small" @click="inBed(scope.$index, scope.row)">床位信息
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.page"
                    :page-size="query.limit" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
            </div>
        </div>

        <!-- 添加弹出框 -->
        <el-dialog title="添加" v-model="addVisible" width="50%">
            <el-form label-width="110px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="姓名">
                            <el-input v-model="createForm.hz_name"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="职业">
                            <el-input v-model="createForm.hz_career"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="addVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveCreate">确 定</el-button>
                </span>
            </template>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" v-model="editVisible" width="50%">
            <el-form label-width="110px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="姓名">
                            <el-input v-model="eidtForm.info.hz_name"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="职业">
                            <el-input v-model="eidtForm.info.hz_career"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>

        <!-- 入院信息 -->
        <el-dialog title="床位信息" v-model="patientVisible" width="50%">
            <el-form label-width="110px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="病床">
                            <el-select v-model="patientForm.info.bed_id" class="m-2" placeholder="请选择床位" size="large">
                                <el-option v-for="item in beds" :key="item.bed_id"
                                    :label="`${item.depart_name}病区-${item.room_name}房-${item.bed_name}床`"
                                    :value="item.bed_id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="patientVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveBed">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
    import { ref, reactive } from "vue";
    import { ElMessage, ElMessageBox } from "element-plus";
    import { departAll, staffAll, emptyBed, bedAll, alarmList, alarmUpdate, alarmCreate, alarmBed, clearBed } from "../api/index";
    import { typeName, log, getUserInfo } from "../utils/user"

    export default {
        name: "basetable",
        setup() {
            const statusText = ["禁用", "启用"]
            const typeNames = typeName();

            let typeText = {};
            Object.keys(typeNames).forEach(k => {
                typeText[typeNames[k][0]] = typeNames[k][1]
            })
            const query = reactive({
                keywords: "",
                depart: '0',
                page: 1,
                limit: 15,
            });
            let user = getUserInfo();
            const departs = ref([]);

            const tableData = ref([]);
            const pageTotal = ref(0);

            // 获取表格数据
            const getData = () => {
                alarmList(query).then((res) => {
                    if (res.status == 'ok') {
                        tableData.value = res.data.list.list;
                        //过滤下departs
                        if (departs.value.length == 0) {
                            if (user.is_admin == '0') {
                                let allDeparts = user.manage_departs ? user.manage_departs.split(',') : []
                                res.data.depart.forEach(depart => {
                                    if (allDeparts.indexOf(depart.depart_id + '') > -1) {
                                        departs.value.push(depart)
                                    }
                                })
                            } else {
                                departs.value = res.data.depart
                            }
                        }

                        pageTotal.value = res.data.list.pageTotal || 1;
                    } else {
                        ElMessage.error(res.msg)
                        return false;
                    }
                });
            };
            getData();

            // 查询操作
            const handleSearch = () => {
                query.page = 1;
                getData();
            };
            // 分页导航
            const handlePageChange = (val) => {
                query.page = val;
                getData();
            };

            // 删除操作
            const handleDelete = (index) => {
                // 二次确认删除
                ElMessageBox.confirm("确定要删除吗？", "提示", {
                    type: "warning",
                })
                    .then(() => {
                        ElMessage.success("删除成功");
                        tableData.value.splice(index, 1);
                    })
                    .catch(() => { });
            };

            // 表格添加
            const addVisible = ref(false);

            const handleCreate = () => {
                if (departs.value.length == 0) {
                    departAll({}).then(res => {
                        if (res.status == 'ok') {
                            departs.value = res.data;
                            log(departs)
                            addVisible.value = true;
                        } else {
                            ElMessage.error(res.msg)
                            return false;
                        }
                    })
                } else {
                    addVisible.value = true;
                }
            };
            const shapes = [
                {
                    id: "1",
                    title: "实习医生"
                },
                {
                    id: "2",
                    title: "处方医生"
                },
                {
                    id: "3",
                    title: "护士"
                },
                {
                    id: "4",
                    title: "收费人员"
                },
                {
                    id: "5",
                    title: "药房人员"
                },
                {
                    id: "6",
                    title: "系统维护"
                }
            ]
            const getShape = id => {
                let shape = shapes.find(v => v.id == id);
                return shape.title
            }
            const contactTypes = [
                "本人",
                "监护人",
                "父母",
                "子女",
                "亲戚",
                "朋友",
            ]
            let createForm = reactive({
                hz_name: "",
                hz_sex: "男",
                hz_blood: "",
                hz_country: "",
                hz_nation: "",
                hz_career: "",
                hz_union_type: "身份证",
                hz_union_type_diy: "",
                hz_union_id: "",
                hz_union_add: "",
                hz_birthdate: "",
                hz_contact_type: "",
                hz_contact_name: "",
                hz_contact_iphone: "",
                hz_contact_uuid: "",
                hz_contact_add: "",
            });

            const saveCreate = () => {
                log(createForm)
                if (!createForm.hz_name) {
                    ElMessage.error('请输入姓名')
                    return false;
                }

                patientCreate(createForm).then(res => {
                    if (res.status == 'ok') {
                        addVisible.value = false;
                        ElMessage.success(res.msg);
                        query.pageIndex = 1;
                        getData();
                    } else {
                        ElMessage.error(res.msg)
                    }
                })
            }


            // 表格编辑时弹窗和保存
            const editVisible = ref(false);
            let eidtForm = reactive({
                id: 0,
                info: {
                    hz_name: "",
                    hz_sex: "男",
                    hz_blood: "",
                    hz_country: "",
                    hz_nation: "",
                    hz_career: "",
                    hz_union_type: "",
                    hz_union_type_diy: "",
                    hz_union_id: "",
                    hz_union_add: "",
                    hz_birthdate: "",
                    hz_contact_type: "",
                    hz_contact_type_diy: "",
                    hz_contact_name: "",
                    hz_contact_iphone: "",
                    hz_contact_uuid: "",
                    hz_contact_add: "",
                }
            });
            let idx = -1;
            const handleEdit = (index, row) => {
                idx = index;
                eidtForm.info.hz_name = row["hz_name"];
                eidtForm.info.hz_sex = row["hz_sex"];
                eidtForm.info.hz_blood = row["hz_blood"];
                eidtForm.info.hz_country = row["hz_country"];
                eidtForm.info.hz_nation = row["hz_nation"];
                eidtForm.info.hz_career = row["hz_career"];

                eidtForm.info.hz_union_type = row["hz_union_type"];
                eidtForm.info.hz_union_type_diy = row["hz_union_type"];
                eidtForm.info.hz_union_id = row["hz_union_id"];
                eidtForm.info.hz_union_add = row["hz_union_add"];
                eidtForm.info.hz_birthdate = row["hz_birthdate"];
                eidtForm.info.hz_contact_type = row["hz_contact_type"];
                eidtForm.info.hz_contact_type_diy = row["hz_contact_type"];
                eidtForm.info.hz_contact_name = row["hz_contact_name"];
                eidtForm.info.hz_contact_iphone = row["hz_contact_iphone"];
                eidtForm.info.hz_contact_uuid = row["hz_contact_uuid"];
                eidtForm.info.hz_contact_add = row["hz_contact_add"];

                eidtForm.id = row["inpatient_id"];
                if (departs.value.length == 0) {
                    departAll({}).then(res => {
                        if (res.status == 'ok') {
                            departs.value = res.data;
                            editVisible.value = true;
                        } else {
                            ElMessage.error(res.msg)
                            return false;
                        }
                    })
                } else {
                    editVisible.value = true;
                }
            };
            const saveEdit = () => {
                if (!eidtForm.info.hz_name) {
                    ElMessage.error('请输入姓名')
                    return false;
                }
                patientUpdate(eidtForm).then(res => {
                    if (res.status == 'ok') {
                        editVisible.value = false;
                        ElMessage.success(res.msg);
                        query.pageIndex = 1;
                        getData();
                    } else {
                        ElMessage.error(res.msg)
                    }
                })

            };
            const toggleStatus = (id, status) => {
                log("toggleStatus", id)
                let temp = {
                    id: id,
                    info: {
                        status: status
                    }
                }

                ElMessageBox.confirm("确认修改吗", "提示", {
                    type: "warning",
                })
                    .then(() => {
                        accountUpdate(temp).then(res => {
                            if (res.status == 'ok') {
                                ElMessage.success(res.msg)
                                getData();
                                return false;
                            } else {
                                ElMessage.error(res.msg)
                                return false;
                            }
                        })
                    })
                    .catch(() => { });


            }
            const feeTypes = [
                '自费',
                '医保',
                '统筹',
                '记账',
                '其他'
            ]
            const staffs = ref([]);
            const beds = ref([]);
            const patientVisible = ref(false);
            let patientForm = reactive({
                id: 0,
                info: {
                    bed_id: "",
                }
            });
            const inBed = (index, row) => {
                patientForm.info.bed_id = row["bed_id"];
                patientForm.id = row["alarm_id"];
                bedAll({}).then(res => {
                    if (res.status == 'ok') {
                        beds.value = res.data;
                        patientVisible.value = true;
                    } else {
                        ElMessage.error(res.msg)
                        return false;
                    }
                })
            }
            const saveBed = () => {
                if (!patientForm.info.bed_id) {
                    ElMessage.error('请选择床位')
                    return false;
                }
                alarmBed(patientForm).then(res => {
                    log("res",res)
                    if (res.status == 'ok') {
                        patientVisible.value = false;
                        beds.value = []
                        ElMessage.success(res.msg);
                        getData();
                    } else {
                        ElMessage.error(res.msg)
                    }
                })

            };

            return {
                statusText,
                typeText,

                // data
                query,
                tableData,
                pageTotal,


                handleSearch,
                handlePageChange,
                handleDelete,

                contactTypes,
                editVisible,
                eidtForm,
                handleEdit,
                saveEdit,

                createForm,
                addVisible,
                handleCreate,
                saveCreate,

                toggleStatus,
                departs,
                inBed,
                patientVisible,
                patientForm,
                staffs,
                beds,
                feeTypes,
                saveBed,
            };
        },
    };
</script>

<style scoped>
    .handle-box {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 8px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }

    .m-radio {
        margin: 12px;
    }
</style>